#!/bin/bash
# 接收解压路径参数
extract_path=$1

# 设置一些参数
target_ip='192.168.2.11'
j3_timesync_dir='/backhaul'
algorithms_and_types="-o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa"

# del the old one
echo "Deleting old ~/toolchains and recreating it..."
echo "rebuild path: $HOME/toolchains"
rm -rf "$HOME/toolchains"
mkdir -p "$HOME/toolchains"
sync
sleep 1

# 1. copy all scripts
echo "Copying all scripts to ~/toolchains"
cp -rf "$extract_path/toolchain_deploy/"*.sh "$HOME/toolchains"

# 2. extract toolchains.tar.gz
echo "Extracting toolchains.tar.gz..."
tar -xf "$extract_path/toolchain_deploy/toolchains.tar.xz" -C "$extract_path/toolchain_deploy"
cp -rf "$extract_path/toolchain_deploy/toolchains" "$HOME/toolchains"

# 3. copy timesync into j3
echo "Installing timesync..."
ssh root@${target_ip} "mkdir -p ${j3_timesync_dir}/simple_timesync;"
scp "$extract_path/toolchain_deploy/toolchains/simple_timesync/simple_timesync" root@${target_ip}:${j3_timesync_dir}/simple_timesync
scp "$extract_path/toolchain_deploy/toolchains/simple_timesync/start_ts_j3.sh" root@${target_ip}:${j3_timesync_dir}/simple_timesync
ssh "${algorithms_and_types}" root@${target_ip} "/sbin/reboot"

# 4. install dz-viewer
echo "Installing dz-viewer..."
cd "$extract_path/toolchain_deploy/toolchains" || exit 255
sudo dpkg -r dz-viewer-pilot3.0
sudo dpkg -i dz-viewer-pilot3.0.deb

# 5. install DrivingTestRecorder
echo "Installing DrivingTestRecorder..."
sudo dpkg -r DrivingTestRecorder
sudo dpkg -i DrivingTestRecorder.deb

# 6. install auto_trigger
echo "Installing auto_trigger..."
tar -axf auto_trigger.tar.gz
rm -rf "$HOME/auto_trigger"
mv auto_trigger "$HOME/"

# 删除解压路径
echo "Removing extract path: $extract_path ..."
rm -fr "$extract_path"

echo "Installation completed."
